{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Crop images from mask\n",
    "\n",
    "This example, show how to get cropped images from a segmentation mask. For this example we are using this butterfly dataset: http://www.josiahwang.com/dataset/leedsbutterfly/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import json\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0060050.png\n",
      "0060087.png\n",
      "0060093.png\n",
      "0100029.png\n",
      "0070055.png\n",
      "0020104.png\n",
      "0020110.png\n",
      "0070069.png\n",
      "0020138.png\n",
      "0040155.png\n",
      "0070096.png\n",
      "0090162.png\n",
      "0010004.png\n",
      "0010010.png\n",
      "0090176.png\n",
      "0040141.png\n",
      "0070082.png\n",
      "0040169.png\n",
      "0010038.png\n",
      "0070121.png\n",
      "0020070.png\n",
      "0020064.png\n",
      "0020058.png\n",
      "0040035.png\n",
      "0040009.png\n",
      "0030061.png\n",
      "0060118.png\n",
      "0030049.png\n",
      "0080007.png\n",
      "0100175.png\n",
      "0050030.png\n",
      "0050024.png\n",
      "0080013.png\n",
      "0100149.png\n",
      "0050018.png\n",
      "0050019.png\n",
      "0100148.png\n",
      "0100160.png\n",
      "0050025.png\n",
      "0090201.png\n",
      "0080012.png\n",
      "0090215.png\n",
      "0080006.png\n",
      "0050031.png\n",
      "0030048.png\n",
      "0060125.png\n",
      "0030060.png\n",
      "0060131.png\n",
      "0040034.png\n",
      "0070108.png\n",
      "0020071.png\n",
      "0040168.png\n",
      "0010039.png\n",
      "0090177.png\n",
      "0010011.png\n",
      "0070083.png\n",
      "0070097.png\n",
      "0010005.png\n",
      "0090188.png\n",
      "0070068.png\n",
      "0020139.png\n",
      "0020111.png\n",
      "0020105.png\n",
      "0100028.png\n",
      "0060092.png\n",
      "0060086.png\n",
      "0060045.png\n",
      "0060047.png\n",
      "0100016.png\n",
      "0060084.png\n",
      "0070056.png\n",
      "0040142.png\n",
      "0070081.png\n",
      "0010013.png\n",
      "0090175.png\n",
      "0010007.png\n",
      "0020073.png\n",
      "0040036.png\n",
      "0090001.png\n",
      "0040022.png\n",
      "0020098.png\n",
      "0060127.png\n",
      "0030062.png\n",
      "0080010.png\n",
      "0090203.png\n",
      "0050027.png\n",
      "0100162.png\n",
      "0100176.png\n",
      "0080004.png\n",
      "0080038.png\n",
      "0080039.png\n",
      "0050032.png\n",
      "0080005.png\n",
      "0100163.png\n",
      "0050026.png\n",
      "0060126.png\n",
      "0090028.png\n",
      "0090014.png\n",
      "0040037.png\n",
      "0020072.png\n",
      "0070123.png\n",
      "0010006.png\n",
      "0090160.png\n",
      "0070094.png\n",
      "0040157.png\n",
      "0070080.png\n",
      "0040143.png\n",
      "0010012.png\n",
      "0070057.png\n",
      "0070043.png\n",
      "0020112.png\n",
      "0100003.png\n",
      "0060091.png\n",
      "0060046.png\n",
      "0060056.png\n",
      "0070047.png\n",
      "0070053.png\n",
      "0070084.png\n",
      "0010002.png\n",
      "0020076.png\n",
      "0090038.png\n",
      "0040033.png\n",
      "0090010.png\n",
      "0060136.png\n",
      "0080029.png\n",
      "0050022.png\n",
      "0090212.png\n",
      "0080001.png\n",
      "0100173.png\n",
      "0050036.png\n",
      "0050037.png\n",
      "0100172.png\n",
      "0100166.png\n",
      "0050023.png\n",
      "0080014.png\n",
      "0080028.png\n",
      "0060123.png\n",
      "0090011.png\n",
      "0020088.png\n",
      "0090039.png\n",
      "0020063.png\n",
      "0040152.png\n",
      "0090165.png\n",
      "0010017.png\n",
      "0040146.png\n",
      "0070085.png\n",
      "0070052.png\n",
      "0020117.png\n",
      "0070046.png\n",
      "0060080.png\n",
      "0100006.png\n",
      "0100012.png\n",
      "0060094.png\n",
      "0060043.png\n",
      "0060041.png\n",
      "0100038.png\n",
      "0070078.png\n",
      "0020101.png\n",
      "0070050.png\n",
      "0070044.png\n",
      "0020115.png\n",
      "0010029.png\n",
      "0010001.png\n",
      "0070093.png\n",
      "0040144.png\n",
      "0010015.png\n",
      "0070124.png\n",
      "0020061.png\n",
      "0040018.png\n",
      "0090013.png\n",
      "0040024.png\n",
      "0040030.png\n",
      "0030058.png\n",
      "0050009.png\n",
      "0050035.png\n",
      "0080002.png\n",
      "0080016.png\n",
      "0050021.png\n",
      "0080017.png\n",
      "0090204.png\n",
      "0100165.png\n",
      "0050020.png\n",
      "0050034.png\n",
      "0100171.png\n",
      "0080003.png\n",
      "0050008.png\n",
      "0060120.png\n",
      "0060108.png\n",
      "0030059.png\n",
      "0040031.png\n",
      "0090006.png\n",
      "0040019.png\n",
      "0020060.png\n",
      "0020074.png\n",
      "0040145.png\n",
      "0070086.png\n",
      "0010014.png\n",
      "0070092.png\n",
      "0010028.png\n",
      "0070045.png\n",
      "0070079.png\n",
      "0020128.png\n",
      "0060097.png\n",
      "0100005.png\n",
      "0100039.png\n",
      "0060054.png\n",
      "0100089.png\n",
      "0020167.png\n",
      "0070036.png\n",
      "0070022.png\n",
      "0010067.png\n",
      "0040136.png\n",
      "0040122.png\n",
      "0090115.png\n",
      "0010073.png\n",
      "0090129.png\n",
      "0040095.png\n",
      "0040042.png\n",
      "0040056.png\n",
      "0090061.png\n",
      "0090049.png\n",
      "0060153.png\n",
      "0050090.png\n",
      "0030002.png\n",
      "0030016.png\n",
      "0060147.png\n",
      "0050084.png\n",
      "0050047.png\n",
      "0080058.png\n",
      "0080059.png\n",
      "0050046.png\n",
      "0060185.png\n",
      "0050052.png\n",
      "0030017.png\n",
      "0050085.png\n",
      "0060146.png\n",
      "0050091.png\n",
      "0060152.png\n",
      "0030003.png\n",
      "0040057.png\n",
      "0090060.png\n",
      "0010072.png\n",
      "0090114.png\n",
      "0090100.png\n",
      "0010066.png\n",
      "0060218.png\n",
      "0070037.png\n",
      "0060224.png\n",
      "0100077.png\n",
      "0060026.png\n",
      "0060030.png\n",
      "0020164.png\n",
      "0090116.png\n",
      "0010070.png\n",
      "0040121.png\n",
      "0010064.png\n",
      "0010058.png\n",
      "0020004.png\n",
      "0040082.png\n",
      "0090062.png\n",
      "0040055.png\n",
      "0090076.png\n",
      "0040069.png\n",
      "0050087.png\n",
      "0030015.png\n",
      "0030001.png\n",
      "0060150.png\n",
      "0030029.png\n",
      "0050044.png\n",
      "0060193.png\n",
      "0050050.png\n",
      "0100115.png\n",
      "0050078.png\n",
      "0100128.png\n",
      "0050079.png\n",
      "0050051.png\n",
      "0050045.png\n",
      "0060186.png\n",
      "0060179.png\n",
      "0030028.png\n",
      "0060151.png\n",
      "0050086.png\n",
      "0060145.png\n",
      "0030014.png\n",
      "0040068.png\n",
      "0090088.png\n",
      "0020039.png\n",
      "0010059.png\n",
      "0040134.png\n",
      "0090103.png\n",
      "0010065.png\n",
      "0010071.png\n",
      "0090117.png\n",
      "0040120.png\n",
      "0070008.png\n",
      "0020159.png\n",
      "0070034.png\n",
      "0020171.png\n",
      "0070020.png\n",
      "0060025.png\n",
      "0060009.png\n",
      "0060021.png\n",
      "0100058.png\n",
      "0070024.png\n",
      "0020175.png\n",
      "0020161.png\n",
      "0070030.png\n",
      "0010049.png\n",
      "0040124.png\n",
      "0010075.png\n",
      "0010061.png\n",
      "0090107.png\n",
      "0040130.png\n",
      "0090098.png\n",
      "0020015.png\n",
      "0040087.png\n",
      "0040078.png\n",
      "0090067.png\n",
      "0040044.png\n",
      "0030038.png\n",
      "0030010.png\n",
      "0050082.png\n",
      "0030004.png\n",
      "0050069.png\n",
      "0050041.png\n",
      "0050055.png\n",
      "0100110.png\n",
      "0100111.png\n",
      "0060197.png\n",
      "0050054.png\n",
      "0060183.png\n",
      "0050040.png\n",
      "0050068.png\n",
      "0060154.png\n",
      "0030005.png\n",
      "0030011.png\n",
      "0050083.png\n",
      "0030039.png\n",
      "0040045.png\n",
      "0040051.png\n",
      "0020028.png\n",
      "0090099.png\n",
      "0010060.png\n",
      "0010074.png\n",
      "0040119.png\n",
      "0010048.png\n",
      "0020160.png\n",
      "0070025.png\n",
      "0020174.png\n",
      "0070019.png\n",
      "0060034.png\n",
      "0060036.png\n",
      "0100067.png\n",
      "0070033.png\n",
      "0020176.png\n",
      "0070027.png\n",
      "0040133.png\n",
      "0010062.png\n",
      "0090110.png\n",
      "0010076.png\n",
      "0040127.png\n",
      "0040084.png\n",
      "0020016.png\n",
      "0040047.png\n",
      "0030007.png\n",
      "0050081.png\n",
      "0060142.png\n",
      "0030013.png\n",
      "0080049.png\n",
      "0050056.png\n",
      "0060195.png\n",
      "0100113.png\n",
      "0100107.png\n",
      "0050042.png\n",
      "0060180.png\n",
      "0050043.png\n",
      "0060194.png\n",
      "0050057.png\n",
      "0080048.png\n",
      "0050080.png\n",
      "0030012.png\n",
      "0030006.png\n",
      "0060157.png\n",
      "0090065.png\n",
      "0040052.png\n",
      "0040046.png\n",
      "0020003.png\n",
      "0020017.png\n",
      "0010077.png\n",
      "0090105.png\n",
      "0010063.png\n",
      "0070026.png\n",
      "0070032.png\n",
      "0060209.png\n",
      "0100072.png\n",
      "0060037.png\n",
      "0060023.png\n",
      "0100099.png\n",
      "0010085.png\n",
      "0020146.png\n",
      "0060204.png\n",
      "0070017.png\n",
      "0070003.png\n",
      "0010046.png\n",
      "0040117.png\n",
      "0010052.png\n",
      "0090134.png\n",
      "0090097.png\n",
      "0020026.png\n",
      "0040088.png\n",
      "0090054.png\n",
      "0060172.png\n",
      "0030023.png\n",
      "0030037.png\n",
      "0060166.png\n",
      "0100137.png\n",
      "0050072.png\n",
      "0080045.png\n",
      "0080051.png\n",
      "0050066.png\n",
      "0100123.png\n",
      "0060199.png\n",
      "0060198.png\n",
      "0080050.png\n",
      "0100122.png\n",
      "0050067.png\n",
      "0050073.png\n",
      "0100136.png\n",
      "0080044.png\n",
      "0030036.png\n",
      "0030022.png\n",
      "0090069.png\n",
      "0090041.png\n",
      "0040062.png\n",
      "0040089.png\n",
      "0020027.png\n",
      "0090096.png\n",
      "0020033.png\n",
      "0090109.png\n",
      "0040102.png\n",
      "0090135.png\n",
      "0010053.png\n",
      "0010047.png\n",
      "0020153.png\n",
      "0020147.png\n",
      "0010084.png\n",
      "0070016.png\n",
      "0100042.png\n",
      "0060007.png\n",
      "0100097.png\n",
      "0060039.png\n",
      "0100054.png\n",
      "0020151.png\n",
      "0070014.png\n",
      "0010086.png\n",
      "0020145.png\n",
      "0010051.png\n",
      "0040114.png\n",
      "0090123.png\n",
      "0010045.png\n",
      "0020186.png\n",
      "0040128.png\n",
      "0010079.png\n",
      "0020031.png\n",
      "0040074.png\n",
      "0040048.png\n",
      "0030034.png\n",
      "0030020.png\n",
      "0060171.png\n",
      "0060159.png\n",
      "0030008.png\n",
      "0050065.png\n",
      "0100120.png\n",
      "0080052.png\n",
      "0080046.png\n",
      "0100134.png\n",
      "0050071.png\n",
      "0050059.png\n",
      "0050058.png\n",
      "0080047.png\n",
      "0050070.png\n",
      "0050064.png\n",
      "0080053.png\n",
      "0030009.png\n",
      "0030021.png\n",
      "0060164.png\n",
      "0030035.png\n",
      "0040049.png\n",
      "0040061.png\n",
      "0020030.png\n",
      "0020024.png\n",
      "0010078.png\n",
      "0010044.png\n",
      "0090122.png\n",
      "0090136.png\n",
      "0010050.png\n",
      "0070029.png\n",
      "0060206.png\n",
      "0020144.png\n",
      "0070001.png\n",
      "0060212.png\n",
      "0100041.png\n",
      "0060038.png\n",
      "0100082.png\n",
      "0100096.png\n",
      "0100092.png\n",
      "0100086.png\n",
      "0100079.png\n",
      "0100051.png\n",
      "0100045.png\n",
      "0070039.png\n",
      "0020168.png\n",
      "0070005.png\n",
      "0020154.png\n",
      "0010083.png\n",
      "0070011.png\n",
      "0010068.png\n",
      "0040105.png\n",
      "0010054.png\n",
      "0090132.png\n",
      "0090126.png\n",
      "0010040.png\n",
      "0040111.png\n",
      "0020008.png\n",
      "0020020.png\n",
      "0020034.png\n",
      "0040071.png\n",
      "0040065.png\n",
      "0030031.png\n",
      "0060160.png\n",
      "0030025.png\n",
      "0100119.png\n",
      "0050048.png\n",
      "0080057.png\n",
      "0050060.png\n",
      "0050074.png\n",
      "0080043.png\n",
      "0050075.png\n",
      "0100130.png\n",
      "0080042.png\n",
      "0100124.png\n",
      "0050061.png\n",
      "0050049.png\n",
      "0060175.png\n",
      "0030024.png\n",
      "0030030.png\n",
      "0030018.png\n",
      "0090053.png\n",
      "0040064.png\n",
      "0040070.png\n",
      "0040058.png\n",
      "0020021.png\n",
      "0090084.png\n",
      "0010041.png\n",
      "0090127.png\n",
      "0040110.png\n",
      "0040104.png\n",
      "0010055.png\n",
      "0040138.png\n",
      "0010069.png\n",
      "0010082.png\n",
      "0020141.png\n",
      "0070010.png\n",
      "0060217.png\n",
      "0070004.png\n",
      "0020155.png\n",
      "0070038.png\n",
      "0020169.png\n",
      "0100044.png\n",
      "0100050.png\n",
      "0100093.png\n",
      "0060029.png\n",
      "0100091.png\n",
      "0070012.png\n",
      "0020143.png\n",
      "0070006.png\n",
      "0090119.png\n",
      "0090125.png\n",
      "0010043.png\n",
      "0010057.png\n",
      "0040099.png\n",
      "0020037.png\n",
      "0090092.png\n",
      "0090086.png\n",
      "0020023.png\n",
      "0040066.png\n",
      "0050088.png\n",
      "0030026.png\n",
      "0060177.png\n",
      "0060163.png\n",
      "0030032.png\n",
      "0060188.png\n",
      "0080040.png\n",
      "0100132.png\n",
      "0050077.png\n",
      "0050063.png\n",
      "0100126.png\n",
      "0080054.png\n",
      "0050062.png\n",
      "0080055.png\n",
      "0080041.png\n",
      "0050076.png\n",
      "0100133.png\n",
      "0030033.png\n",
      "0030027.png\n",
      "0050089.png\n",
      "0090044.png\n",
      "0040073.png\n",
      "0090078.png\n",
      "0090087.png\n",
      "0040098.png\n",
      "0090130.png\n",
      "0010056.png\n",
      "0040113.png\n",
      "0010042.png\n",
      "0020181.png\n",
      "0090118.png\n",
      "0060214.png\n",
      "0070007.png\n",
      "0060200.png\n",
      "0020142.png\n",
      "0060228.png\n",
      "0100047.png\n",
      "0060059.png\n",
      "0100020.png\n",
      "0100034.png\n",
      "0100008.png\n",
      "0070074.png\n",
      "0090180.png\n",
      "0020125.png\n",
      "0070060.png\n",
      "0020119.png\n",
      "0010025.png\n",
      "0090143.png\n",
      "0090157.png\n",
      "0010031.png\n",
      "0010019.png\n",
      "0070100.png\n",
      "0070114.png\n",
      "0020092.png\n",
      "0030040.png\n",
      "0060105.png\n",
      "0030054.png\n",
      "0060139.png\n",
      "0080026.png\n",
      "0050011.png\n",
      "0100154.png\n",
      "0050005.png\n",
      "0080032.png\n",
      "0090221.png\n",
      "0050039.png\n",
      "0050038.png\n",
      "0050004.png\n",
      "0080033.png\n",
      "0080027.png\n",
      "0100155.png\n",
      "0050010.png\n",
      "0060138.png\n",
      "0030055.png\n",
      "0030041.png\n",
      "0020087.png\n",
      "0090022.png\n",
      "0040015.png\n",
      "0040001.png\n",
      "0020078.png\n",
      "0020044.png\n",
      "0070115.png\n",
      "0070101.png\n",
      "0020050.png\n",
      "0040149.png\n",
      "0010018.png\n",
      "0010030.png\n",
      "0090156.png\n",
      "0040161.png\n",
      "0010024.png\n",
      "0070061.png\n",
      "0070075.png\n",
      "0100009.png\n",
      "0060072.png\n",
      "0100037.png\n",
      "0100023.png\n",
      "0070063.png\n",
      "0010032.png\n",
      "0010026.png\n",
      "0090140.png\n",
      "0070088.png\n",
      "0070117.png\n",
      "0020046.png\n",
      "0020052.png\n",
      "0070103.png\n",
      "0040017.png\n",
      "0020085.png\n",
      "0020091.png\n",
      "0040003.png\n",
      "0030057.png\n",
      "0060106.png\n",
      "0030043.png\n",
      "0080031.png\n",
      "0100143.png\n",
      "0050006.png\n",
      "0050012.png\n",
      "0100157.png\n",
      "0080025.png\n",
      "0080019.png\n",
      "0080018.png\n",
      "0050013.png\n",
      "0080024.png\n",
      "0090223.png\n",
      "0080030.png\n",
      "0050007.png\n",
      "0060113.png\n",
      "0030042.png\n",
      "0030056.png\n",
      "0060107.png\n",
      "0040002.png\n",
      "0070102.png\n",
      "0010027.png\n",
      "0010033.png\n",
      "0090182.png\n",
      "0070076.png\n",
      "0070062.png\n",
      "0020133.png\n",
      "0060098.png\n",
      "0100022.png\n",
      "0100036.png\n",
      "0060077.png\n",
      "0060063.png\n",
      "0060088.png\n",
      "0070066.png\n",
      "0070072.png\n",
      "0070099.png\n",
      "0010037.png\n",
      "0010023.png\n",
      "0090145.png\n",
      "0070112.png\n",
      "0070106.png\n",
      "0020057.png\n",
      "0030052.png\n",
      "0030046.png\n",
      "0060117.png\n",
      "0080008.png\n",
      "0050003.png\n",
      "0080034.png\n",
      "0080020.png\n",
      "0080021.png\n",
      "0100153.png\n",
      "0050016.png\n",
      "0050002.png\n",
      "0100147.png\n",
      "0080035.png\n",
      "0090226.png\n",
      "0080009.png\n",
      "0030047.png\n",
      "0030053.png\n",
      "0020095.png\n",
      "0020081.png\n",
      "0090024.png\n",
      "0040013.png\n",
      "0070107.png\n",
      "0020042.png\n",
      "0090144.png\n",
      "0010022.png\n",
      "0010036.png\n",
      "0090178.png\n",
      "0070098.png\n",
      "0020136.png\n",
      "0060048.png\n",
      "0060074.png\n",
      "0100019.png\n",
      "0100025.png\n",
      "0100031.png\n",
      "0070059.png\n",
      "0020120.png\n",
      "0070065.png\n",
      "0090191.png\n",
      "0040159.png\n",
      "0010008.png\n",
      "0010020.png\n",
      "0090146.png\n",
      "0090152.png\n",
      "0010034.png\n",
      "0020054.png\n",
      "0070105.png\n",
      "0070111.png\n",
      "0020040.png\n",
      "0090032.png\n",
      "0060128.png\n",
      "0030045.png\n",
      "0030051.png\n",
      "0060100.png\n",
      "0050028.png\n",
      "0050014.png\n",
      "0100151.png\n",
      "0080037.png\n",
      "0100145.png\n",
      "0080036.png\n",
      "0090225.png\n",
      "0050001.png\n",
      "0100150.png\n",
      "0050015.png\n",
      "0080022.png\n",
      "0090219.png\n",
      "0050029.png\n",
      "0030050.png\n",
      "0060115.png\n",
      "0030044.png\n",
      "0060129.png\n",
      "0040010.png\n",
      "0020082.png\n",
      "0090027.png\n",
      "0090033.png\n",
      "0020096.png\n",
      "0040038.png\n",
      "0070110.png\n",
      "0020041.png\n",
      "0020055.png\n",
      "0070104.png\n",
      "0040164.png\n",
      "0010035.png\n",
      "0090147.png\n",
      "0010021.png\n",
      "0040170.png\n",
      "0040158.png\n",
      "0010009.png\n",
      "0070064.png\n",
      "0020135.png\n",
      "0020121.png\n",
      "0090184.png\n",
      "0100030.png\n",
      "0100024.png\n",
      "0100018.png\n",
      "0060049.png\n"
     ]
    }
   ],
   "source": [
    "PATH = 'raw_data/images/'\n",
    "MASK_PATH = 'raw_data/segmentations/'\n",
    "RESULT_PATH = 'data/objects/'\n",
    "\n",
    "f = []\n",
    "for (dirpath, dirnames, filenames) in os.walk(PATH):\n",
    "    f.extend(filenames)\n",
    "    break\n",
    "    \n",
    "keys = []\n",
    "ext = '.png'\n",
    "for name in f:\n",
    "    if(name.endswith(ext)):\n",
    "        print(name)\n",
    "        keys.append(name[0:len(name)-len(ext)])\n",
    "    \n",
    "if not os.path.exists(RESULT_PATH):\n",
    "    os.makedirs(RESULT_PATH)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0060050\n",
      "image save\n",
      "0060087\n",
      "image save\n",
      "0060093\n",
      "image save\n",
      "0100029\n",
      "image save\n",
      "0070055\n",
      "image save\n",
      "0020104\n",
      "image save\n",
      "0020110\n",
      "image save\n",
      "0070069\n",
      "image save\n",
      "0020138\n",
      "image save\n",
      "0040155\n",
      "image save\n",
      "0070096\n",
      "image save\n",
      "0090162\n",
      "image save\n",
      "0010004\n",
      "image save\n",
      "0010010\n",
      "image save\n",
      "0090176\n",
      "image save\n",
      "0040141\n",
      "image save\n",
      "0070082\n",
      "image save\n",
      "0040169\n",
      "image save\n",
      "0010038\n",
      "image save\n",
      "0070121\n",
      "image save\n",
      "0020070\n",
      "image save\n",
      "0020064\n",
      "image save\n",
      "0020058\n",
      "image save\n",
      "0040035\n",
      "image save\n",
      "0040009\n",
      "image save\n",
      "0030061\n",
      "image save\n",
      "0060118\n",
      "image save\n",
      "0030049\n",
      "image save\n",
      "0080007\n",
      "image save\n",
      "0100175\n",
      "image save\n",
      "0050030\n",
      "image save\n",
      "0050024\n",
      "image save\n",
      "0080013\n",
      "image save\n",
      "0100149\n",
      "image save\n",
      "0050018\n",
      "image save\n",
      "0050019\n",
      "image save\n",
      "0100148\n",
      "image save\n",
      "0100160\n",
      "image save\n",
      "0050025\n",
      "image save\n",
      "0090201\n",
      "image save\n",
      "0080012\n",
      "image save\n",
      "0090215\n",
      "image save\n",
      "0080006\n",
      "image save\n",
      "0050031\n",
      "image save\n",
      "0030048\n",
      "image save\n",
      "0060125\n",
      "image save\n",
      "0030060\n",
      "image save\n",
      "0060131\n",
      "image save\n",
      "0040034\n",
      "image save\n",
      "0070108\n",
      "image save\n",
      "0020071\n",
      "image save\n",
      "0040168\n",
      "image save\n",
      "0010039\n",
      "image save\n",
      "0090177\n",
      "image save\n",
      "0010011\n",
      "image save\n",
      "0070083\n",
      "image save\n",
      "0070097\n",
      "image save\n",
      "0010005\n",
      "image save\n",
      "0090188\n",
      "image save\n",
      "0070068\n",
      "image save\n",
      "0020139\n",
      "image save\n",
      "0020111\n",
      "image save\n",
      "0020105\n",
      "image save\n",
      "0100028\n",
      "image save\n",
      "0060092\n",
      "image save\n",
      "0060086\n",
      "image save\n",
      "0060045\n",
      "image save\n",
      "0060047\n",
      "image save\n",
      "0100016\n",
      "image save\n",
      "0060084\n",
      "image save\n",
      "0070056\n",
      "image save\n",
      "0040142\n",
      "image save\n",
      "0070081\n",
      "image save\n",
      "0010013\n",
      "image save\n",
      "0090175\n",
      "image save\n",
      "0010007\n",
      "image save\n",
      "0020073\n",
      "image save\n",
      "0040036\n",
      "image save\n",
      "0090001\n",
      "image save\n",
      "0040022\n",
      "image save\n",
      "0020098\n",
      "image save\n",
      "0060127\n",
      "image save\n",
      "0030062\n",
      "image save\n",
      "0080010\n",
      "image save\n",
      "0090203\n",
      "image save\n",
      "0050027\n",
      "image save\n",
      "0100162\n",
      "image save\n",
      "0100176\n",
      "image save\n",
      "0080004\n",
      "image save\n",
      "0080038\n",
      "image save\n",
      "0080039\n",
      "image save\n",
      "0050032\n",
      "image save\n",
      "0080005\n",
      "image save\n",
      "0100163\n",
      "image save\n",
      "0050026\n",
      "image save\n",
      "0060126\n",
      "image save\n",
      "0090028\n",
      "image save\n",
      "0090014\n",
      "image save\n",
      "0040037\n",
      "image save\n",
      "0020072\n",
      "image save\n",
      "0070123\n",
      "image save\n",
      "0010006\n",
      "image save\n",
      "0090160\n",
      "image save\n",
      "0070094\n",
      "image save\n",
      "0040157\n",
      "image save\n",
      "0070080\n",
      "image save\n",
      "0040143\n",
      "image save\n",
      "0010012\n",
      "image save\n",
      "0070057\n",
      "image save\n",
      "0070043\n",
      "image save\n",
      "0020112\n",
      "image save\n",
      "0100003\n",
      "image save\n",
      "0060091\n",
      "image save\n",
      "0060046\n",
      "image save\n",
      "0060056\n",
      "image save\n",
      "0070047\n",
      "image save\n",
      "0070053\n",
      "image save\n",
      "0070084\n",
      "image save\n",
      "0010002\n",
      "image save\n",
      "0020076\n",
      "image save\n",
      "0090038\n",
      "image save\n",
      "0040033\n",
      "image save\n",
      "0090010\n",
      "image save\n",
      "0060136\n",
      "image save\n",
      "0080029\n",
      "image save\n",
      "0050022\n",
      "image save\n",
      "0090212\n",
      "image save\n",
      "0080001\n",
      "image save\n",
      "0100173\n",
      "image save\n",
      "0050036\n",
      "image save\n",
      "0050037\n",
      "image save\n",
      "0100172\n",
      "image save\n",
      "0100166\n",
      "image save\n",
      "0050023\n",
      "image save\n",
      "0080014\n",
      "image save\n",
      "0080028\n",
      "image save\n",
      "0060123\n",
      "image save\n",
      "0090011\n",
      "image save\n",
      "0020088\n",
      "image save\n",
      "0090039\n",
      "image save\n",
      "0020063\n",
      "image save\n",
      "0040152\n",
      "image save\n",
      "0090165\n",
      "image save\n",
      "0010017\n",
      "image save\n",
      "0040146\n",
      "image save\n",
      "0070085\n",
      "image save\n",
      "0070052\n",
      "image save\n",
      "0020117\n",
      "image save\n",
      "0070046\n",
      "image save\n",
      "0060080\n",
      "image save\n",
      "0100006\n",
      "image save\n",
      "0100012\n",
      "image save\n",
      "0060094\n",
      "image save\n",
      "0060043\n",
      "image save\n",
      "0060041\n",
      "image save\n",
      "0100038\n",
      "image save\n",
      "0070078\n",
      "image save\n",
      "0020101\n",
      "image save\n",
      "0070050\n",
      "image save\n",
      "0070044\n",
      "image save\n",
      "0020115\n",
      "image save\n",
      "0010029\n",
      "image save\n",
      "0010001\n",
      "image save\n",
      "0070093\n",
      "image save\n",
      "0040144\n",
      "image save\n",
      "0010015\n",
      "image save\n",
      "0070124\n",
      "image save\n",
      "0020061\n",
      "image save\n",
      "0040018\n",
      "image save\n",
      "0090013\n",
      "image save\n",
      "0040024\n",
      "image save\n",
      "0040030\n",
      "image save\n",
      "0030058\n",
      "image save\n",
      "0050009\n",
      "image save\n",
      "0050035\n",
      "image save\n",
      "0080002\n",
      "image save\n",
      "0080016\n",
      "image save\n",
      "0050021\n",
      "image save\n",
      "0080017\n",
      "image save\n",
      "0090204\n",
      "image save\n",
      "0100165\n",
      "image save\n",
      "0050020\n",
      "image save\n",
      "0050034\n",
      "image save\n",
      "0100171\n",
      "image save\n",
      "0080003\n",
      "image save\n",
      "0050008\n",
      "image save\n",
      "0060120\n",
      "image save\n",
      "0060108\n",
      "image save\n",
      "0030059\n",
      "image save\n",
      "0040031\n",
      "image save\n",
      "0090006\n",
      "image save\n",
      "0040019\n",
      "image save\n",
      "0020060\n",
      "image save\n",
      "0020074\n",
      "image save\n",
      "0040145\n",
      "image save\n",
      "0070086\n",
      "image save\n",
      "0010014\n",
      "image save\n",
      "0070092\n",
      "image save\n",
      "0010028\n",
      "image save\n",
      "0070045\n",
      "image save\n",
      "0070079\n",
      "image save\n",
      "0020128\n",
      "image save\n",
      "0060097\n",
      "image save\n",
      "0100005\n",
      "image save\n",
      "0100039\n",
      "image save\n",
      "0060054\n",
      "image save\n",
      "0100089\n",
      "image save\n",
      "0020167\n",
      "image save\n",
      "0070036\n",
      "image save\n",
      "0070022\n",
      "image save\n",
      "0010067\n",
      "image save\n",
      "0040136\n",
      "image save\n",
      "0040122\n",
      "image save\n",
      "0090115\n",
      "image save\n",
      "0010073\n",
      "image save\n",
      "0090129\n",
      "image save\n",
      "0040095\n",
      "image save\n",
      "0040042\n",
      "image save\n",
      "0040056\n",
      "image save\n",
      "0090061\n",
      "image save\n",
      "0090049\n",
      "image save\n",
      "0060153\n",
      "image save\n",
      "0050090\n",
      "image save\n",
      "0030002\n",
      "image save\n",
      "0030016\n",
      "image save\n",
      "0060147\n",
      "image save\n",
      "0050084\n",
      "image save\n",
      "0050047\n",
      "image save\n",
      "0080058\n",
      "image save\n",
      "0080059\n",
      "image save\n",
      "0050046\n",
      "image save\n",
      "0060185\n",
      "image save\n",
      "0050052\n",
      "image save\n",
      "0030017\n",
      "image save\n",
      "0050085\n",
      "image save\n",
      "0060146\n",
      "image save\n",
      "0050091\n",
      "image save\n",
      "0060152\n",
      "image save\n",
      "0030003\n",
      "image save\n",
      "0040057\n",
      "image save\n",
      "0090060\n",
      "image save\n",
      "0010072\n",
      "image save\n",
      "0090114\n",
      "image save\n",
      "0090100\n",
      "image save\n",
      "0010066\n",
      "image save\n",
      "0060218\n",
      "image save\n",
      "0070037\n",
      "image save\n",
      "0060224\n",
      "image save\n",
      "0100077\n",
      "image save\n",
      "0060026\n",
      "image save\n",
      "0060030\n",
      "image save\n",
      "0020164\n",
      "image save\n",
      "0090116\n",
      "image save\n",
      "0010070\n",
      "image save\n",
      "0040121\n",
      "image save\n",
      "0010064\n",
      "image save\n",
      "0010058\n",
      "image save\n",
      "0020004\n",
      "image save\n",
      "0040082\n",
      "image save\n",
      "0090062\n",
      "image save\n",
      "0040055\n",
      "image save\n",
      "0090076\n",
      "image save\n",
      "0040069\n",
      "image save\n",
      "0050087\n",
      "image save\n",
      "0030015\n",
      "image save\n",
      "0030001\n",
      "image save\n",
      "0060150\n",
      "image save\n",
      "0030029\n",
      "image save\n",
      "0050044\n",
      "image save\n",
      "0060193\n",
      "image save\n",
      "0050050\n",
      "image save\n",
      "0100115\n",
      "image save\n",
      "0050078\n",
      "image save\n",
      "0100128\n",
      "image save\n",
      "0050079\n",
      "image save\n",
      "0050051\n",
      "image save\n",
      "0050045\n",
      "image save\n",
      "0060186\n",
      "image save\n",
      "0060179\n",
      "image save\n",
      "0030028\n",
      "image save\n",
      "0060151\n",
      "image save\n",
      "0050086\n",
      "image save\n",
      "0060145\n",
      "image save\n",
      "0030014\n",
      "image save\n",
      "0040068\n",
      "image save\n",
      "0090088\n",
      "image save\n",
      "0020039\n",
      "image save\n",
      "0010059\n",
      "image save\n",
      "0040134\n",
      "image save\n",
      "0090103\n",
      "image save\n",
      "0010065\n",
      "image save\n",
      "0010071\n",
      "image save\n",
      "0090117\n",
      "image save\n",
      "0040120\n",
      "image save\n",
      "0070008\n",
      "image save\n",
      "0020159\n",
      "image save\n",
      "0070034\n",
      "image save\n",
      "0020171\n",
      "image save\n",
      "0070020\n",
      "image save\n",
      "0060025\n",
      "image save\n",
      "0060009\n",
      "image save\n",
      "0060021\n",
      "image save\n",
      "0100058\n",
      "image save\n",
      "0070024\n",
      "image save\n",
      "0020175\n",
      "image save\n",
      "0020161\n",
      "image save\n",
      "0070030\n",
      "image save\n",
      "0010049\n",
      "image save\n",
      "0040124\n",
      "image save\n",
      "0010075\n",
      "image save\n",
      "0010061\n",
      "image save\n",
      "0090107\n",
      "image save\n",
      "0040130\n",
      "image save\n",
      "0090098\n",
      "image save\n",
      "0020015\n",
      "image save\n",
      "0040087\n",
      "image save\n",
      "0040078\n",
      "image save\n",
      "0090067\n",
      "image save\n",
      "0040044\n",
      "image save\n",
      "0030038\n",
      "image save\n",
      "0030010\n",
      "image save\n",
      "0050082\n",
      "image save\n",
      "0030004\n",
      "image save\n",
      "0050069\n",
      "image save\n",
      "0050041\n",
      "image save\n",
      "0050055\n",
      "image save\n",
      "0100110\n",
      "image save\n",
      "0100111\n",
      "image save\n",
      "0060197\n",
      "image save\n",
      "0050054\n",
      "image save\n",
      "0060183\n",
      "image save\n",
      "0050040\n",
      "image save\n",
      "0050068\n",
      "image save\n",
      "0060154\n",
      "image save\n",
      "0030005\n",
      "image save\n",
      "0030011\n",
      "image save\n",
      "0050083\n",
      "image save\n",
      "0030039\n",
      "image save\n",
      "0040045\n",
      "image save\n",
      "0040051\n",
      "image save\n",
      "0020028\n",
      "image save\n",
      "0090099\n",
      "image save\n",
      "0010060\n",
      "image save\n",
      "0010074\n",
      "image save\n",
      "0040119\n",
      "image save\n",
      "0010048\n",
      "image save\n",
      "0020160\n",
      "image save\n",
      "0070025\n",
      "image save\n",
      "0020174\n",
      "image save\n",
      "0070019\n",
      "image save\n",
      "0060034\n",
      "image save\n",
      "0060036\n",
      "image save\n",
      "0100067\n",
      "image save\n",
      "0070033\n",
      "image save\n",
      "0020176\n",
      "image save\n",
      "0070027\n",
      "image save\n",
      "0040133\n",
      "image save\n",
      "0010062\n",
      "image save\n",
      "0090110\n",
      "image save\n",
      "0010076\n",
      "image save\n",
      "0040127\n",
      "image save\n",
      "0040084\n",
      "image save\n",
      "0020016\n",
      "image save\n",
      "0040047\n",
      "image save\n",
      "0030007\n",
      "image save\n",
      "0050081\n",
      "image save\n",
      "0060142\n",
      "image save\n",
      "0030013\n",
      "image save\n",
      "0080049\n",
      "image save\n",
      "0050056\n",
      "image save\n",
      "0060195\n",
      "image save\n",
      "0100113\n",
      "image save\n",
      "0100107\n",
      "image save\n",
      "0050042\n",
      "image save\n",
      "0060180\n",
      "image save\n",
      "0050043\n",
      "image save\n",
      "0060194\n",
      "image save\n",
      "0050057\n",
      "image save\n",
      "0080048\n",
      "image save\n",
      "0050080\n",
      "image save\n",
      "0030012\n",
      "image save\n",
      "0030006\n",
      "image save\n",
      "0060157\n",
      "image save\n",
      "0090065\n",
      "image save\n",
      "0040052\n",
      "image save\n",
      "0040046\n",
      "image save\n",
      "0020003\n",
      "image save\n",
      "0020017\n",
      "image save\n",
      "0010077\n",
      "image save\n",
      "0090105\n",
      "image save\n",
      "0010063\n",
      "image save\n",
      "0070026\n",
      "image save\n",
      "0070032\n",
      "image save\n",
      "0060209\n",
      "image save\n",
      "0100072\n",
      "image save\n",
      "0060037\n",
      "image save\n",
      "0060023\n",
      "image save\n",
      "0100099\n",
      "image save\n",
      "0010085\n",
      "image save\n",
      "0020146\n",
      "image save\n",
      "0060204\n",
      "image save\n",
      "0070017\n",
      "image save\n",
      "0070003\n",
      "image save\n",
      "0010046\n",
      "image save\n",
      "0040117\n",
      "image save\n",
      "0010052\n",
      "image save\n",
      "0090134\n",
      "image save\n",
      "0090097\n",
      "image save\n",
      "0020026\n",
      "image save\n",
      "0040088\n",
      "image save\n",
      "0090054\n",
      "image save\n",
      "0060172\n",
      "image save\n",
      "0030023\n",
      "image save\n",
      "0030037\n",
      "image save\n",
      "0060166\n",
      "image save\n",
      "0100137\n",
      "image save\n",
      "0050072\n",
      "image save\n",
      "0080045\n",
      "image save\n",
      "0080051\n",
      "image save\n",
      "0050066\n",
      "image save\n",
      "0100123\n",
      "image save\n",
      "0060199\n",
      "image save\n",
      "0060198\n",
      "image save\n",
      "0080050\n",
      "image save\n",
      "0100122\n",
      "image save\n",
      "0050067\n",
      "image save\n",
      "0050073\n",
      "image save\n",
      "0100136\n",
      "image save\n",
      "0080044\n",
      "image save\n",
      "0030036\n",
      "image save\n",
      "0030022\n",
      "image save\n",
      "0090069\n",
      "image save\n",
      "0090041\n",
      "image save\n",
      "0040062\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "image save\n",
      "0040089\n",
      "image save\n",
      "0020027\n",
      "image save\n",
      "0090096\n",
      "image save\n",
      "0020033\n",
      "image save\n",
      "0090109\n",
      "image save\n",
      "0040102\n",
      "image save\n",
      "0090135\n",
      "image save\n",
      "0010053\n",
      "image save\n",
      "0010047\n",
      "image save\n",
      "0020153\n",
      "image save\n",
      "0020147\n",
      "image save\n",
      "0010084\n",
      "image save\n",
      "0070016\n",
      "image save\n",
      "0100042\n",
      "image save\n",
      "0060007\n",
      "image save\n",
      "0100097\n",
      "image save\n",
      "0060039\n",
      "image save\n",
      "0100054\n",
      "image save\n",
      "0020151\n",
      "image save\n",
      "0070014\n",
      "image save\n",
      "0010086\n",
      "image save\n",
      "0020145\n",
      "image save\n",
      "0010051\n",
      "image save\n",
      "0040114\n",
      "image save\n",
      "0090123\n",
      "image save\n",
      "0010045\n",
      "image save\n",
      "0020186\n",
      "image save\n",
      "0040128\n",
      "image save\n",
      "0010079\n",
      "image save\n",
      "0020031\n",
      "image save\n",
      "0040074\n",
      "image save\n",
      "0040048\n",
      "image save\n",
      "0030034\n",
      "image save\n",
      "0030020\n",
      "image save\n",
      "0060171\n",
      "image save\n",
      "0060159\n",
      "image save\n",
      "0030008\n",
      "image save\n",
      "0050065\n",
      "image save\n",
      "0100120\n",
      "image save\n",
      "0080052\n",
      "image save\n",
      "0080046\n",
      "image save\n",
      "0100134\n",
      "image save\n",
      "0050071\n",
      "image save\n",
      "0050059\n",
      "image save\n",
      "0050058\n",
      "image save\n",
      "0080047\n",
      "image save\n",
      "0050070\n",
      "image save\n",
      "0050064\n",
      "image save\n",
      "0080053\n",
      "image save\n",
      "0030009\n",
      "image save\n",
      "0030021\n",
      "image save\n",
      "0060164\n",
      "image save\n",
      "0030035\n",
      "image save\n",
      "0040049\n",
      "image save\n",
      "0040061\n",
      "image save\n",
      "0020030\n",
      "image save\n",
      "0020024\n",
      "image save\n",
      "0010078\n",
      "image save\n",
      "0010044\n",
      "image save\n",
      "0090122\n",
      "image save\n",
      "0090136\n",
      "image save\n",
      "0010050\n",
      "image save\n",
      "0070029\n",
      "image save\n",
      "0060206\n",
      "image save\n",
      "0020144\n",
      "image save\n",
      "0070001\n",
      "image save\n",
      "0060212\n",
      "image save\n",
      "0100041\n",
      "image save\n",
      "0060038\n",
      "image save\n",
      "0100082\n",
      "image save\n",
      "0100096\n",
      "image save\n",
      "0100092\n",
      "image save\n",
      "0100086\n",
      "image save\n",
      "0100079\n",
      "image save\n",
      "0100051\n",
      "image save\n",
      "0100045\n",
      "image save\n",
      "0070039\n",
      "image save\n",
      "0020168\n",
      "image save\n",
      "0070005\n",
      "image save\n",
      "0020154\n",
      "image save\n",
      "0010083\n",
      "image save\n",
      "0070011\n",
      "image save\n",
      "0010068\n",
      "image save\n",
      "0040105\n",
      "image save\n",
      "0010054\n",
      "image save\n",
      "0090132\n",
      "image save\n",
      "0090126\n",
      "image save\n",
      "0010040\n",
      "image save\n",
      "0040111\n",
      "image save\n",
      "0020008\n",
      "image save\n",
      "0020020\n",
      "image save\n",
      "0020034\n",
      "image save\n",
      "0040071\n",
      "image save\n",
      "0040065\n",
      "image save\n",
      "0030031\n",
      "image save\n",
      "0060160\n",
      "image save\n",
      "0030025\n",
      "image save\n",
      "0100119\n",
      "image save\n",
      "0050048\n",
      "image save\n",
      "0080057\n",
      "image save\n",
      "0050060\n",
      "image save\n",
      "0050074\n",
      "image save\n",
      "0080043\n",
      "image save\n",
      "0050075\n",
      "image save\n",
      "0100130\n",
      "image save\n",
      "0080042\n",
      "image save\n",
      "0100124\n",
      "image save\n",
      "0050061\n",
      "image save\n",
      "0050049\n",
      "image save\n",
      "0060175\n",
      "image save\n",
      "0030024\n",
      "image save\n",
      "0030030\n",
      "image save\n",
      "0030018\n",
      "image save\n",
      "0090053\n",
      "image save\n",
      "0040064\n",
      "image save\n",
      "0040070\n",
      "image save\n",
      "0040058\n",
      "image save\n",
      "0020021\n",
      "image save\n",
      "0090084\n",
      "image save\n",
      "0010041\n",
      "image save\n",
      "0090127\n",
      "image save\n",
      "0040110\n",
      "image save\n",
      "0040104\n",
      "image save\n",
      "0010055\n",
      "image save\n",
      "0040138\n",
      "image save\n",
      "0010069\n",
      "image save\n",
      "0010082\n",
      "image save\n",
      "0020141\n",
      "image save\n",
      "0070010\n",
      "image save\n",
      "0060217\n",
      "image save\n",
      "0070004\n",
      "image save\n",
      "0020155\n",
      "image save\n",
      "0070038\n",
      "image save\n",
      "0020169\n",
      "image save\n",
      "0100044\n",
      "image save\n",
      "0100050\n",
      "image save\n",
      "0100093\n",
      "image save\n",
      "0060029\n",
      "image save\n",
      "0100091\n",
      "image save\n",
      "0070012\n",
      "image save\n",
      "0020143\n",
      "image save\n",
      "0070006\n",
      "image save\n",
      "0090119\n",
      "image save\n",
      "0090125\n",
      "image save\n",
      "0010043\n",
      "image save\n",
      "0010057\n",
      "image save\n",
      "0040099\n",
      "image save\n",
      "0020037\n",
      "image save\n",
      "0090092\n",
      "image save\n",
      "0090086\n",
      "image save\n",
      "0020023\n",
      "image save\n",
      "0040066\n",
      "image save\n",
      "0050088\n",
      "image save\n",
      "0030026\n",
      "image save\n",
      "0060177\n",
      "image save\n",
      "0060163\n",
      "image save\n",
      "0030032\n",
      "image save\n",
      "0060188\n",
      "image save\n",
      "0080040\n",
      "image save\n",
      "0100132\n",
      "image save\n",
      "0050077\n",
      "image save\n",
      "0050063\n",
      "image save\n",
      "0100126\n",
      "image save\n",
      "0080054\n",
      "image save\n",
      "0050062\n",
      "image save\n",
      "0080055\n",
      "image save\n",
      "0080041\n",
      "image save\n",
      "0050076\n",
      "image save\n",
      "0100133\n",
      "image save\n",
      "0030033\n",
      "image save\n",
      "0030027\n",
      "image save\n",
      "0050089\n",
      "image save\n",
      "0090044\n",
      "image save\n",
      "0040073\n",
      "image save\n",
      "0090078\n",
      "image save\n",
      "0090087\n",
      "image save\n",
      "0040098\n",
      "image save\n",
      "0090130\n",
      "image save\n",
      "0010056\n",
      "image save\n",
      "0040113\n",
      "image save\n",
      "0010042\n",
      "image save\n",
      "0020181\n",
      "image save\n",
      "0090118\n",
      "image save\n",
      "0060214\n",
      "image save\n",
      "0070007\n",
      "image save\n",
      "0060200\n",
      "image save\n",
      "0020142\n",
      "image save\n",
      "0060228\n",
      "image save\n",
      "0100047\n",
      "image save\n",
      "0060059\n",
      "image save\n",
      "0100020\n",
      "image save\n",
      "0100034\n",
      "image save\n",
      "0100008\n",
      "image save\n",
      "0070074\n",
      "image save\n",
      "0090180\n",
      "image save\n",
      "0020125\n",
      "image save\n",
      "0070060\n",
      "image save\n",
      "0020119\n",
      "image save\n",
      "0010025\n",
      "image save\n",
      "0090143\n",
      "image save\n",
      "0090157\n",
      "image save\n",
      "0010031\n",
      "image save\n",
      "0010019\n",
      "image save\n",
      "0070100\n",
      "image save\n",
      "0070114\n",
      "image save\n",
      "0020092\n",
      "image save\n",
      "0030040\n",
      "image save\n",
      "0060105\n",
      "image save\n",
      "0030054\n",
      "image save\n",
      "0060139\n",
      "image save\n",
      "0080026\n",
      "image save\n",
      "0050011\n",
      "image save\n",
      "0100154\n",
      "image save\n",
      "0050005\n",
      "image save\n",
      "0080032\n",
      "image save\n",
      "0090221\n",
      "image save\n",
      "0050039\n",
      "image save\n",
      "0050038\n",
      "image save\n",
      "0050004\n",
      "image save\n",
      "0080033\n",
      "image save\n",
      "0080027\n",
      "image save\n",
      "0100155\n",
      "image save\n",
      "0050010\n",
      "image save\n",
      "0060138\n",
      "image save\n",
      "0030055\n",
      "image save\n",
      "0030041\n",
      "image save\n",
      "0020087\n",
      "image save\n",
      "0090022\n",
      "image save\n",
      "0040015\n",
      "image save\n",
      "0040001\n",
      "image save\n",
      "0020078\n",
      "image save\n",
      "0020044\n",
      "image save\n",
      "0070115\n",
      "image save\n",
      "0070101\n",
      "image save\n",
      "0020050\n",
      "image save\n",
      "0040149\n",
      "image save\n",
      "0010018\n",
      "image save\n",
      "0010030\n",
      "image save\n",
      "0090156\n",
      "image save\n",
      "0040161\n",
      "image save\n",
      "0010024\n",
      "image save\n",
      "0070061\n",
      "image save\n",
      "0070075\n",
      "image save\n",
      "0100009\n",
      "image save\n",
      "0060072\n",
      "image save\n",
      "0100037\n",
      "image save\n",
      "0100023\n",
      "image save\n",
      "0070063\n",
      "image save\n",
      "0010032\n",
      "image save\n",
      "0010026\n",
      "image save\n",
      "0090140\n",
      "image save\n",
      "0070088\n",
      "image save\n",
      "0070117\n",
      "image save\n",
      "0020046\n",
      "image save\n",
      "0020052\n",
      "image save\n",
      "0070103\n",
      "image save\n",
      "0040017\n",
      "image save\n",
      "0020085\n",
      "image save\n",
      "0020091\n",
      "image save\n",
      "0040003\n",
      "image save\n",
      "0030057\n",
      "image save\n",
      "0060106\n",
      "image save\n",
      "0030043\n",
      "image save\n",
      "0080031\n",
      "image save\n",
      "0100143\n",
      "image save\n",
      "0050006\n",
      "image save\n",
      "0050012\n",
      "image save\n",
      "0100157\n",
      "image save\n",
      "0080025\n",
      "image save\n",
      "0080019\n",
      "image save\n",
      "0080018\n",
      "image save\n",
      "0050013\n",
      "image save\n",
      "0080024\n",
      "image save\n",
      "0090223\n",
      "image save\n",
      "0080030\n",
      "image save\n",
      "0050007\n",
      "image save\n",
      "0060113\n",
      "image save\n",
      "0030042\n",
      "image save\n",
      "0030056\n",
      "image save\n",
      "0060107\n",
      "image save\n",
      "0040002\n",
      "image save\n",
      "0070102\n",
      "image save\n",
      "0010027\n",
      "image save\n",
      "0010033\n",
      "image save\n",
      "0090182\n",
      "image save\n",
      "0070076\n",
      "image save\n",
      "0070062\n",
      "image save\n",
      "0020133\n",
      "image save\n",
      "0060098\n",
      "image save\n",
      "0100022\n",
      "image save\n",
      "0100036\n",
      "image save\n",
      "0060077\n",
      "image save\n",
      "0060063\n",
      "image save\n",
      "0060088\n",
      "image save\n",
      "0070066\n",
      "image save\n",
      "0070072\n",
      "image save\n",
      "0070099\n",
      "image save\n",
      "0010037\n",
      "image save\n",
      "0010023\n",
      "image save\n",
      "0090145\n",
      "image save\n",
      "0070112\n",
      "image save\n",
      "0070106\n",
      "image save\n",
      "0020057\n",
      "image save\n",
      "0030052\n",
      "image save\n",
      "0030046\n",
      "image save\n",
      "0060117\n",
      "image save\n",
      "0080008\n",
      "image save\n",
      "0050003\n",
      "image save\n",
      "0080034\n",
      "image save\n",
      "0080020\n",
      "image save\n",
      "0080021\n",
      "image save\n",
      "0100153\n",
      "image save\n",
      "0050016\n",
      "image save\n",
      "0050002\n",
      "image save\n",
      "0100147\n",
      "image save\n",
      "0080035\n",
      "image save\n",
      "0090226\n",
      "image save\n",
      "0080009\n",
      "image save\n",
      "0030047\n",
      "image save\n",
      "0030053\n",
      "image save\n",
      "0020095\n",
      "image save\n",
      "0020081\n",
      "image save\n",
      "0090024\n",
      "image save\n",
      "0040013\n",
      "image save\n",
      "0070107\n",
      "image save\n",
      "0020042\n",
      "image save\n",
      "0090144\n",
      "image save\n",
      "0010022\n",
      "image save\n",
      "0010036\n",
      "image save\n",
      "0090178\n",
      "image save\n",
      "0070098\n",
      "image save\n",
      "0020136\n",
      "image save\n",
      "0060048\n",
      "image save\n",
      "0060074\n",
      "image save\n",
      "0100019\n",
      "image save\n",
      "0100025\n",
      "image save\n",
      "0100031\n",
      "image save\n",
      "0070059\n",
      "image save\n",
      "0020120\n",
      "image save\n",
      "0070065\n",
      "image save\n",
      "0090191\n",
      "image save\n",
      "0040159\n",
      "image save\n",
      "0010008\n",
      "image save\n",
      "0010020\n",
      "image save\n",
      "0090146\n",
      "image save\n",
      "0090152\n",
      "image save\n",
      "0010034\n",
      "image save\n",
      "0020054\n",
      "image save\n",
      "0070105\n",
      "image save\n",
      "0070111\n",
      "image save\n",
      "0020040\n",
      "image save\n",
      "0090032\n",
      "image save\n",
      "0060128\n",
      "image save\n",
      "0030045\n",
      "image save\n",
      "0030051\n",
      "image save\n",
      "0060100\n",
      "image save\n",
      "0050028\n",
      "image save\n",
      "0050014\n",
      "image save\n",
      "0100151\n",
      "image save\n",
      "0080037\n",
      "image save\n",
      "0100145\n",
      "image save\n",
      "0080036\n",
      "image save\n",
      "0090225\n",
      "image save\n",
      "0050001\n",
      "image save\n",
      "0100150\n",
      "image save\n",
      "0050015\n",
      "image save\n",
      "0080022\n",
      "image save\n",
      "0090219\n",
      "image save\n",
      "0050029\n",
      "image save\n",
      "0030050\n",
      "image save\n",
      "0060115\n",
      "image save\n",
      "0030044\n",
      "image save\n",
      "0060129\n",
      "image save\n",
      "0040010\n",
      "image save\n",
      "0020082\n",
      "image save\n",
      "0090027\n",
      "image save\n",
      "0090033\n",
      "image save\n",
      "0020096\n",
      "image save\n",
      "0040038\n",
      "image save\n",
      "0070110\n",
      "image save\n",
      "0020041\n",
      "image save\n",
      "0020055\n",
      "image save\n",
      "0070104\n",
      "image save\n",
      "0040164\n",
      "image save\n",
      "0010035\n",
      "image save\n",
      "0090147\n",
      "image save\n",
      "0010021\n",
      "image save\n",
      "0040170\n",
      "image save\n",
      "0040158\n",
      "image save\n",
      "0010009\n",
      "image save\n",
      "0070064\n",
      "image save\n",
      "0020135\n",
      "image save\n",
      "0020121\n",
      "image save\n",
      "0090184\n",
      "image save\n",
      "0100030\n",
      "image save\n",
      "0100024\n",
      "image save\n",
      "0100018\n",
      "image save\n",
      "0060049\n",
      "image save\n"
     ]
    }
   ],
   "source": [
    "for key in keys:\n",
    "    print(key)\n",
    "    img = cv2.imread('{}{}.png'.format(PATH, key))\n",
    "    mask = cv2.imread('{}{}.png'.format(MASK_PATH, '{}_seg0'.format(key)))\n",
    "    \n",
    "    a, _, _ = cv2.split(mask)\n",
    "\n",
    "    # Get image from mask\n",
    "    mask_out = cv2.subtract(mask, img)\n",
    "    mask_out = cv2.subtract(mask, mask_out)\n",
    "    b_c, g_c, r_c = cv2.split(mask_out)\n",
    "    mask_out = cv2.merge((b_c, g_c, r_c, a))\n",
    "\n",
    "    ## get contours\n",
    "    imgray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)\n",
    "    ret, thresh = cv2.threshold(imgray, 127, 255, 0)\n",
    "    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n",
    "\n",
    "    # Get rect from contour\n",
    "    index = 0\n",
    "    for i in range(len(contours)):\n",
    "        if (len(contours[i]) > len(contours[index])):\n",
    "            index = i\n",
    "    rect = cv2.boundingRect(contours[index])\n",
    "\n",
    "    # Croop image\n",
    "    cropped_img = mask_out[rect[1]:(rect[1]+rect[3]), rect[0]:(rect[0]+rect[2])]\n",
    "\n",
    "#     blank_image = np.zeros((mask.shape[0],mask.shape[1],3), np.uint8)\n",
    "#     cv2.drawContours(blank_image, contours, -1, (0,255,0), 3)\n",
    "\n",
    "#     plt.imshow(blank_image)\n",
    "#     plt.show()\n",
    "\n",
    "#     plt.imshow(cropped_img)\n",
    "#     plt.show()\n",
    "\n",
    "    folder = None\n",
    "    if(key.startswith('001')):\n",
    "        folder = 'danaus_plexippus/'\n",
    "    elif(key.startswith('002')):\n",
    "        folder = 'heliconius_charitonius/'\n",
    "    elif(key.startswith('003')):\n",
    "        folder = 'heliconius_erato/'\n",
    "    elif(key.startswith('004')):\n",
    "        folder = 'junonia_coenia/'\n",
    "    elif(key.startswith('005')):\n",
    "        folder = 'lycaena_phlaeas/'\n",
    "    elif(key.startswith('006')):\n",
    "        folder = 'nymphalis_antiopa/'\n",
    "    elif(key.startswith('007')):\n",
    "        folder = 'papilio_cresphontes/'\n",
    "    elif(key.startswith('008')):\n",
    "        folder = 'pieris_rapae/'\n",
    "    elif(key.startswith('009')):\n",
    "        folder = 'vanessa_atalanta/'\n",
    "    elif(key.startswith('010')):\n",
    "        folder = 'vanessa_cardui/'\n",
    "    \n",
    "    if folder is not None and not os.path.exists('{}{}'.format(RESULT_PATH,folder)):\n",
    "        os.makedirs('{}{}'.format(RESULT_PATH,folder))\n",
    "\n",
    "    cv2.imwrite('{}{}{}.png'.format(RESULT_PATH, folder, key), cropped_img)\n",
    "    print('image saved')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
